package com.ioc.business.job.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ioc.business.job.domain.JobTaskDistribution;
import com.ioc.business.job.domain.bo.task.distribution.JobTaskDistributionBo;
import com.ioc.business.job.domain.bo.task.distribution.JobTaskDistributionQueryBo;
import com.ioc.business.job.mapper.JobTaskDistributionMapper;
import com.ioc.business.job.service.IJobTaskDistributionService;
import com.ioc.business.job.vo.task.distribution.JobTaskDistributionImportVo;
import com.ioc.business.job.vo.task.distribution.JobTaskDistributionPageVo;
import com.ioc.business.job.vo.task.distribution.JobTaskDistributionVo;
import com.ioc.common.constant.UserConstants;
import com.ioc.common.core.page.TableVo;
import com.ioc.common.exception.ServiceException;
import com.ioc.common.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 作业任务分布表 服务实现类
 * </p>
 *
 * @author WeiZhongRong
 * @since 2025-01-08
 */
@Service
@AllArgsConstructor
public class JobTaskDistributionServiceImpl extends ServiceImpl<JobTaskDistributionMapper, JobTaskDistribution> implements IJobTaskDistributionService {
    @Override
    public TableVo<JobTaskDistribution, JobTaskDistributionPageVo> selectJobTaskDistributionPageList(JobTaskDistributionQueryBo queryBo) {
        List<JobTaskDistribution> result = baseMapper
                .queryPageList(queryBo);
        List<JobTaskDistributionPageVo> pageVos = new ArrayList<>(result.size());
        if (CollectionUtil.isNotEmpty(result)) {
            pageVos = buildPageVos(result);
        }
        return new TableVo<>(result, pageVos);
    }

    private List<JobTaskDistributionPageVo> buildPageVos(List<JobTaskDistribution> records) {
        return records.stream().map(record -> {
            JobTaskDistributionPageVo vo = new JobTaskDistributionPageVo();
            BeanUtils.copyProperties(record, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public JobTaskDistributionVo selectJobTaskDistributionById(Long jobTaskDistributionId) {
        JobTaskDistribution jobTaskDistribution = getById(jobTaskDistributionId);
        if (ObjectUtil.isNotEmpty(jobTaskDistribution)) {
            JobTaskDistributionVo jobTaskDistributionVo = new JobTaskDistributionVo();
            BeanUtils.copyProperties(jobTaskDistribution, jobTaskDistributionVo);
            return jobTaskDistributionVo;
        }
        return null;
    }

    /**
     * 新增收货订单
     *
     * @param bo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addJobTaskDistribution(JobTaskDistributionBo bo) {
        JobTaskDistribution jobTaskDistribution = new JobTaskDistribution();
        BeanUtils.copyProperties(bo, jobTaskDistribution);
        jobTaskDistribution.setCreateBy(SecurityUtils.getUsername());
        jobTaskDistribution.setCreateTime(new Date());
        jobTaskDistribution.setUpdateBy(SecurityUtils.getUsername());
        jobTaskDistribution.setUpdateTime(jobTaskDistribution.getCreateTime());
        jobTaskDistribution.setDelFlag(UserConstants.DEL_FLAG_NORMAL);
        return save(jobTaskDistribution);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateJobTaskDistribution(JobTaskDistributionBo bo) {
        JobTaskDistribution jobTaskDistribution = new JobTaskDistribution();
        BeanUtils.copyProperties(bo, jobTaskDistribution);
        jobTaskDistribution.setUpdateBy(SecurityUtils.getUsername());
        jobTaskDistribution.setUpdateTime(jobTaskDistribution.getCreateTime());
        return updateById(jobTaskDistribution);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeJobTaskDistribution(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return false;
        }
        List<JobTaskDistribution> updateList = idList.stream().map(id -> {
            JobTaskDistribution jobTaskDistribution = new JobTaskDistribution();
            jobTaskDistribution.setJobTaskDistributionId(id);
            jobTaskDistribution.setDelFlag(UserConstants.DEL_FLAG_REMOVED);
            return jobTaskDistribution;
        }).collect(Collectors.toList());
        return updateBatchById(updateList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importJobTaskDistribution(List<JobTaskDistributionImportVo> voList) {
        List<JobTaskDistribution> jobTaskDistributionList = verifyImportData(voList);
        saveBatch(jobTaskDistributionList);
    }

    private List<JobTaskDistribution> verifyImportData(List<JobTaskDistributionImportVo> voList) {
        if (CollectionUtil.isEmpty(voList)) {
            throw new ServiceException("导入数据不能为空");
        }
        List<JobTaskDistribution> jobTaskDistributionList = new ArrayList<>(voList.size());
        String username = SecurityUtils.getUsername();
        Date date = new Date();
        for (JobTaskDistributionImportVo jobTaskDistributionImportVo : voList) {
            JobTaskDistribution jobTaskDistribution = new JobTaskDistribution();
            BeanUtils.copyProperties(jobTaskDistributionImportVo, jobTaskDistribution);
            verifyField(jobTaskDistribution, jobTaskDistributionImportVo);
            jobTaskDistribution.setCreateBy(username);
            jobTaskDistribution.setCreateTime(date);
            jobTaskDistribution.setUpdateBy(username);
            jobTaskDistribution.setUpdateTime(date);
            jobTaskDistribution.setDelFlag(UserConstants.DEL_FLAG_NORMAL);
            jobTaskDistributionList.add(jobTaskDistribution);
        }
        return jobTaskDistributionList;
    }

    private void verifyField(JobTaskDistribution jobTaskDistribution, JobTaskDistributionImportVo jobTaskDistributionImportVo) {
        String jobTaskDateStr = jobTaskDistributionImportVo.getJobTaskDate();
        Date jobTaskDate = null;
        try {
            jobTaskDate = StrUtil.isNotBlank(jobTaskDateStr) ? DateUtil.parse(jobTaskDateStr) : null;
        } catch (Exception e) {
            throw new ServiceException("日期格式不正确!");
        }
        jobTaskDistribution.setJobTaskDate(jobTaskDate);
    }
}
